<template>
  <div class="page-distribution-notice">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        label-width="auto"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="msgTitle"
            :label="$t('distributionBind.noticetitle')+':'"
          >
            <el-input
              v-model="searchForm.msgTitle"
              type="text"
              clearable
              :placeholder="$t('distributionBind.noticetitle')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetForm"
            >
              {{ $t('crud.emptyBtn') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <div
          v-if="isAuth('distribution:distributionMsg:save')"
          class="default-btn primary-btn"
          @click.stop="onAddOrUpdate()"
        >
          {{ $t('seckill.newAdd') }}
        </div>

        <div
          v-if="isAuth('distribution:distributionMsg:delete')"
          :class="[dataListSelections.length <= 0 ? 'disabled-btn' :'','default-btn']"
          :disabled="dataListSelections.length <= 0"
          @click.stop="onDelete"
        >
          {{ $t('remindPop.batchDeletion') }}
        </div>
      </div>
      <div class="table-con">
        <el-table
          ref="noticeListTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
          @selection-change="selectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column
            :label="$t('distributionBind.noticetitle')"
            prop="msgTitle"
          >
            <template #default="scope">
              <span class="table-cell-text"> {{ scope.row.msgTitle }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('marketing.effectiveTime')"
            prop="startTime"
            sortable
          />
          <el-table-column
            :label="$t('distributionBind.failureTime')"
            prop="endTime"
            sortable
          />
          <el-table-column
            :label="$t('distribUserWallet.operator')"
            prop="username"
          >
            <template #default="scope">
              {{ scope.row.sysUser?.username || '-' }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('crud.menu')"
            width="220"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('distribution:distributionMsg:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row.msgId)"
                >
                  {{ $t('coupon.edit') }}
                </div>
                <div
                  v-if="isAuth('prod:prod:delete')"
                  class="default-btn text-btn"
                  @click="onDelete(scope.row)"
                >
                  {{ $t('remindPop.delete') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <AddOrUpdate
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import { ElMessageBox, ElMessage } from 'element-plus'
import { isAuth } from '@/utils/index'
import AddOrUpdate from './add-or-update.vue'
import { ref, nextTick, reactive, onMounted } from 'vue'

let tempSearchForm = null // 保存上次点击查询的请求条件

const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  msgTitle: ''
})
const dataList = ref([])
const dataListSelections = ref([])

// 获取数据列表
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/distribution/distributionMsg/page'),
    method: 'get',
    params: http.adornParams(Object.assign({
      current: pageParam ? pageParam.currentPage : 1,
      size: pageParam ? pageParam.pageSize : 10
    }, tempSearchForm))
  }).then(({ data }) => {
    page.total = data.total
    page.pageSize = data.size
    page.currentPage = data.current
    dataList.value = data.records
  })
}
onMounted(() => {
  getDataList()
})

// 刷新回调
const refreshChange = () => {
  getDataList(page)
}

// 删除
const onDelete = (row) => {
  if (!row.msgId && dataListSelections.value.length <= 0) return
  const ids = row.msgId ? [row.msgId] : dataListSelections.value.map(item => {
    return item.msgId
  })
  const tip = $t('remindPop.makeSure') + (row.msgId ? $t('remindPop.delete') : $t('remindPop.batchDeletion')) + $t('text.menu') + '?'
  ElMessageBox.confirm(tip, $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/distribution/distributionMsg'),
      method: 'delete',
      data: http.adornData(ids, false)
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  }).catch(() => { })
}

// 多选回调
const selectionChange = (list) => {
  dataListSelections.value = list
}
// 新增 / 修改
const addOrUpdateRef = ref(null)
const addOrUpdateVisible = ref(false)
const onAddOrUpdate = (msgId) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(msgId)
  })
}

// 点击查询
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}
const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

const searchFormRef = ref(null)
const onResetForm = () => {
  searchFormRef.value.resetFields()
}
</script>
